package travelDream.beans;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

import traveldream.ejb.DTO.ExcursionDTO;
import traveldream.ejb.DTO.HotelDTO;
import traveldream.ejb.DTO.PackageDTO;
import traveldream.ejb.DTO.TransportationDTO;
import traveldream.ejb.interfaces.NotFindException;
import traveldream.ejb.interfaces.PackageBeanLocal;
import traveldream.ejb.interfaces.PackageInsertionBeanLocal;


@ManagedBean(name = "PackageEM")
@ViewScoped
public class PackageEditManaged {
	private boolean notPrima = false;
	
	private List<HotelDTO> hDTO;

	private int selectedH;
	private String selectedNH;
	
	private List<TransportationDTO> tDTO;
	private int selectedG;

	private int selectedR;
	
	private List<ExcursionDTO> eDTO;
	private int selectedE;
	
	private PackageDTO pDTO;
	
	@EJB
	private PackageInsertionBeanLocal PackageInsertionBeanLocal;
	
	@EJB
	private PackageBeanLocal PackageBeanLocal;

	public void allList(){
		hDTO = PackageInsertionBeanLocal.allHotel();
		tDTO = PackageInsertionBeanLocal.allTransport();
		eDTO = PackageInsertionBeanLocal.allExcursion();
	}
	
	private void save(){
		pDTO.setIdGoing(selectedG);
		pDTO.setIdReturn(selectedR);
		pDTO.setIdExcursion(selectedE);
		pDTO.setIdHotel(selectedH);
		
	}
	
	public long dayBetweenTwoDate(Date uno, Date due) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(uno);
		c2.setTime(due);

		long giorni = (c2.getTime().getTime() - c1.getTime().getTime())	/ (24 * 3600 * 1000);

		return giorni;
	}

	public String edit(){
		save();
		try{
		if (PackageInsertionBeanLocal.checkPackage(selectedH,selectedE,selectedG,selectedR)){
			if(PackageInsertionBeanLocal.checkPrice(selectedH,selectedE,selectedG,selectedR,pDTO)){
				PackageBeanLocal.delete(pDTO.getId());
				PackageInsertionBeanLocal.save(pDTO);
				return "package.xhtml?faces-redirect=true";
			}
			else{
				//you can't have a package that costs more than the sum of its basic product
				
				//notify the error
				 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "The price don't match with the basics product"));  
			
			}
		}
		else{
			//notify the error
			 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "The dates and the cities don't match"));  
		}
		}
    	catch(NotFindException e){
    		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "An error occured, please try again!!"));
    	}
		return "";
	}
	
	public void searchById() {
		if(notPrima == false){
			FacesContext facesContext = FacesContext.getCurrentInstance();
			int passedParameter = Integer.parseInt((facesContext.getExternalContext().getRequestParameterMap().get("id")));
		try
		{
			pDTO = PackageBeanLocal.find(passedParameter);
		}
		catch(NotFindException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "The id is wrong, please try again"));
			return;
		}
			selectedG = pDTO.getIdGoing();
			selectedR = pDTO.getIdReturn();
			selectedE = pDTO.getIdExcursion();
			selectedH = pDTO.getIdHotel();
			save();
			for(int i=0;i < hDTO.size();i++){
				if(hDTO.get(i).getId()==selectedH){
					selectedNH = hDTO.get(i).getName();
					break;
				}
			}
			notPrima = true;
		}
	}
	
	
	public List<HotelDTO> gethDTO() {
		return hDTO;
	}

	public void sethDTO(List<HotelDTO> hDTO) {
		this.hDTO = hDTO;
	}

	public int getSelectedH() {
		return selectedH;
	}

	public void setSelectedH(int selectedH) {
		this.selectedH = selectedH;
	}

	public List<TransportationDTO> gettDTO() {
		return tDTO;
	}

	public void settDTO(List<TransportationDTO> tDTO) {
		this.tDTO = tDTO;
	}

	public int getSelectedG() {
		return selectedG;
		
	}

	public void setSelectedG(int selectedG) {
		this.selectedG = selectedG;
	}

	public int getSelectedR() {
		return selectedR;
	}

	public void setSelectedR(int selectedR) {
		this.selectedR = selectedR;
	}

	public List<ExcursionDTO> geteDTO() {
		return eDTO;
	}

	public void seteDTO(List<ExcursionDTO> eDTO) {
		this.eDTO = eDTO;
	}

	public int getSelectedE() {
		return selectedE;
	}

	public void setSelectedE(int selectedE) {
		this.selectedE = selectedE;
	}

	public PackageDTO getpDTO() {
		return pDTO;
	}

	public void setpDTO(PackageDTO pDTO) {
		this.pDTO = pDTO;
	}

	public String getSelectedNH() {
		return selectedNH;
	}

	public void setSelectedNH(String selectedNH) {
		this.selectedNH = selectedNH;
	}
	
	
	
}
